package com.wtwd.campus.receiver;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wtwd.campus.entity.MqAppInfo;
import com.wtwd.campus.entity.MqUpLoadApp;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.UnicodeUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mjy
 * @date 2022/12/12
 * @description
 */
public class UploadAppReceiver implements Receiver {


    @Override
    public String parseMessage(String data) {
        String[] split = data.split(",");
        String imei = split[1];
        String commandNumber = split[3];
        int type = Integer.valueOf(split[4]);
        int groupNum = Integer.valueOf(split[5]);
        String date = split[split.length - 2];
        String deviceStatus = split[split.length - 1];
        MqUpLoadApp mqUpLoadApp = new MqUpLoadApp();
        mqUpLoadApp.setImei(imei);
        mqUpLoadApp.setCommandNumber(commandNumber);
        mqUpLoadApp.setDeviceDate(date);
        mqUpLoadApp.setGroupNum(groupNum);
        mqUpLoadApp.setDeviceTime(commandNumber);
        mqUpLoadApp.setType(type);
        mqUpLoadApp.setDeviceStatus(deviceStatus);
        List<MqAppInfo> mqAppInfos = new ArrayList<>();
        if (groupNum > 0) {
            for (int i = 1; i <= groupNum; i++) {
                String appName = split[5 + 2 * (i - 1) + i];
                String appType = split[6 + 2 * (i - 1) + i];
                String packageName = split[7 + 2 * (i - 1) + i];
                MqAppInfo mqAppInfo = new MqAppInfo();
                mqAppInfo.setAppType(Integer.valueOf(appType));
                mqAppInfo.setAppName(UnicodeUtil.decodeUnicode(appName));
                mqAppInfo.setPackageName(packageName);
                mqAppInfos.add(mqAppInfo);
            }
        }
        mqUpLoadApp.setAppInfos(mqAppInfos);
        InjectFeignService.getIrmFeignClientService().upLoadApp(mqUpLoadApp);
        return imei;
    }

    public static void main(String[] args) {
        String appName = UnicodeUtil.gbEncoding("测试app1");
        System.out.println(appName);
    }
}
